package org.shj.file;

import org.shj.utils.DbUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Shen Huang Jian
 * @date 2020-09-28 19:56
 */
public class GenFile {

    public static void main(String[] args){

        String path = "D:\\test\\hanlp\\xtmainlist\\";

        try {
//            String sql = "select case when ques_type = '108002001' then '城市管理'             \n" +
//                    "\t\t\t\t\t\twhen ques_type = '108002002' then '环境保护' \n" +
//                    "\t\t\t\t\t\twhen ques_type = '108002003' then '安全生产' \n" +
//                    "\t\t\t\t\t\twhen ques_type = '108002004' then '社会治安' \n" +
//                    "\t\t\t\t\t\twhen ques_type = '108002006' then '综合治理' \n" +
//                    "\t\t\t\t\t\twhen ques_type = '108002008' then '其他'\n" +
//                    "\t\t\t\t\t\tELSE '其他' end as ques_type_value, question_desc \n " +
//                    "from (\n" +
//                    "select substring(ques_type_id, 1, 9) ques_type, question_desc \n" +
//                    "from xt_mainlist where ques_type_value is not null and ques_type_value <> '' " +
//                    " and ques_type_id is not null and ques_type_id <> '') tmp;";
//
//
//            genCorpusFile(path, sql);


            String sql2 = "select case when prob_typ='ZHZL' then '综合治理' " +
                    "            when prob_typ='HJBH' then '环境保护' " +
                    " when prob_typ='AQSC' then '安全生产' " +
                    " when prob_typ='CSGL' then '城市管理' " +
                    " when prob_typ='SHZA' then '社会治安' " +
                    " when prob_typ='WMCSCJ' then '文明城市创建' " +
                    " when prob_typ='JRJG' then '金融监管' " +
                    " when prob_typ='TTO' then '三类重点巡查'  " +
                    " ELSE '其他' END as ques_type_value, rmk as question_desc " +
                    "from kwop_wrk_ordr where prob_typ is not null and prob_typ <> '' " +
                    "and rmk is not null and rmk <> ''";

            genCorpusFile(path, sql2);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void processRecord(String folderPath, Map<String, List<CaseInfo>> typeRecordMap){

        for(String type : typeRecordMap.keySet()){
            File typeFolder = new File(folderPath + File.separator + type);
            if(!typeFolder.exists()){
                typeFolder.mkdirs();
            }
            File[] subFiles = typeFolder.listFiles();
            int fileNum = subFiles == null ? 0 : subFiles.length;

            for(CaseInfo caseInfo : typeRecordMap.get(type)){
                File file = new File(folderPath + File.separator + type + File.separator + (++fileNum) + ".txt");
                try(OutputStream os = new FileOutputStream(file)){
                    os.write(caseInfo.getQuestionDesc().getBytes("utf-8"));
                    os.flush();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }


    private static void genCorpusFile(String folderPath, String sql) throws Exception{
        List<CaseInfo> caseInfoList = DbUtils.findBySql(sql, CaseInfo.class);
        Map<String, List<CaseInfo>> caseMap = caseInfoList.stream().collect(Collectors.groupingBy(CaseInfo::getQuesTypeValue));
        processRecord(folderPath, caseMap);

        System.out.println("Files add: ");
        for(String key : caseMap.keySet()){
            System.out.println(key + ": " + caseMap.get(key).size());
        }

        System.out.println("======================");
        System.out.println("Files after added: ");

        File folder = new File(folderPath);
        File[] subFolders = folder.listFiles();
        for(File sub : subFolders){
            System.out.println(sub.getName() + ": " + sub.listFiles().length);
        }
    }

}
